package com.xoa.controller.inv;

import com.xoa.dao.inv.OmReturnMapper;
import com.xoa.dao.inv.OmShipmentMapper;
import com.xoa.model.inv.OmReturn;
import com.xoa.model.inv.OmShipment;
import com.xoa.service.inv.OmReturnService;
import com.xoa.util.ToJson;
import com.xoa.util.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
 * 销售退货OmReturnController
 */
@Controller
@RequestMapping("/salesReturn")
public class OmReturnController {
    @Autowired
    private OmReturnService salesReturnService;
    @Autowired
    private OmReturnMapper omReturnMapper;
    //处理时间  将前台传过来的时间存到数据库中
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
    //映射主页面
    @RequestMapping("/index")
    public String salesReturnGood(HttpServletRequest request) {
        return "/app/storage/salesManagement/salesReturnGoods/salesReturnGoods";
    }
    /**
     * 添加数据
     */
    @RequestMapping("/insertSales")
    @ResponseBody
    public ToJson<Integer> insertSales(HttpServletRequest request, OmReturn omReturn, String productType, String enterName, String enterSpecs, String enterCode) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            if (productType != null && productType != "") {
                map.put("productType", productType);
            }
            if (enterName != null && enterName != "") {
                map.put("enterName", enterName);
            }
            if (enterSpecs != null && enterSpecs != "") {
                map.put("enterSpecs", enterSpecs);
            }
            if (enterCode != null && enterCode != "") {
                map.put("enterCode", enterCode);
            }


            Integer data = salesReturnService.insertSales(omReturn, map);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 查询所有
     *
     * @param request
     * @param page
     * @param useFlag
     * @return
     */
    @RequestMapping("/selectAllSales")
    @ResponseBody
    public ToJson<OmReturn> selectAll(HttpServletRequest request, Integer page, OmReturn omReturn, Integer limit, boolean useFlag, String productType, String enterName, String enterSpecs, String enterCode, Integer type) {
        ToJson json = new ToJson<OmShipment>(1, "err");
        Map<String, Object> map = new HashMap<String, Object>();
        PageParams pageParams = new PageParams();
        pageParams.setUseFlag(useFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(limit);
        map.put("type", type);
        map.put("obj", omReturn);
        map.put("page", pageParams);
        if (productType != null && productType != "") {
            map.put("productType", productType);
        }
        if (enterName != null && enterName != "") {
            map.put("enterName", enterName);
        }
        if (enterSpecs != null && enterSpecs != "") {
            map.put("enterSpecs", enterSpecs);
        }
        if (enterCode != null && enterCode != "") {
            map.put("enterCode", enterCode);
        }
        try {
            List<OmReturn> omReturns = salesReturnService.selectAll(map);
            map.remove("pageParams");
            Integer integer = omReturnMapper.selectAllCount(map);
            if (omReturns != null) {
                json.setTotleNum(integer);
                json.setObj(omReturns);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 数据回显接口
     */
    @RequestMapping("/selectById")
    @ResponseBody
    public ToJson<OmReturn> selectByPrimaryKey(HttpServletRequest request, Integer returnId) {
        ToJson json = new ToJson<OmShipment>(1, "err");
        try {
            OmReturn omReturn = salesReturnService.selectByPrimaryKey(returnId);
            if (omReturn != null) {
                json.setObject(omReturn);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 根据主键id进行删除
     */
    @RequestMapping("/delOmReturnById")
    @ResponseBody
    public ToJson<Integer> deleteByPrimaryKey(HttpServletRequest request, Integer returnId) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Integer data = salesReturnService.deleteByPrimaryKey(returnId);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 更新数据(根据产品id)
     */
    @RequestMapping("/updateReturnById")
    @ResponseBody
    public ToJson<Integer> updateByPrimaryKeySelective(OmReturn omReturn, String productType, String enterName, String enterSpecs, String enterCode) {
        ToJson json = new ToJson<Integer>(1, "err");
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("productType", productType);
            map.put("enterName", enterName);
            map.put("enterSpecs", enterSpecs);
            map.put("enterCode", enterCode);
            Integer data = salesReturnService.updateByPrimaryKeySelective(omReturn, productType,enterName,enterSpecs,enterCode);
            if (data != null) {
                json.setObject(data);
                json.setFlag(0);
                json.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }


    /**
     * 产品信息表的导出
     */
    @RequestMapping(value = "/ExportOmReturn", produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public ToJson<OmReturn> ExportOmReturn(HttpServletRequest request, HttpServletResponse response, OmReturn omReturn, String beginDate, String isExport) {
        return salesReturnService.ExportOmReturn(request, response, omReturn, beginDate, isExport);
    }
    /**
     * 查询所有退货人
     */
    @RequestMapping("/selectUsers")
    @ResponseBody
    public ToJson<OmReturn> selectUsers(HttpServletRequest request, Integer shipmentId) {
        return salesReturnService.selectUsers();
    }
}
